package com.manor.im.http

import com.manor.common.CommonAppConfig
import com.manor.common.http.HttpCallback
import com.manor.common.http.HttpClient
import com.manor.common.http.HttpSignCallback
import com.manor.common.utils.MD5Util
import com.manor.common.utils.ToastUtil
import com.manor.common.interfaces.CommonCallback
import com.manor.common.utils.*
import com.manor.im.bean.GuardPriceBean

/**
 * Created by cxf on 2019/2/26.
 */
 object ImHttpUtil {
    /**
     * 取消网络请求
     */
    @JvmStatic
    fun cancel(tag: String?) {
        HttpClient.getInstance().cancel(tag)
    }

    /**
     * 私信聊天页面用于获取用户信息
     */
    @JvmStatic
    fun getImUserInfo(uids: String?, callback: HttpCallback?) {
        HttpClient.getInstance()["Im.GetMultiInfo", ImHttpConst.GET_IM_USER_INFO]
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("uids", uids)
                .execute(callback)
    }


    /**
     * 获取系统消息列表
     */
    @JvmStatic
    fun getSystemMessageList(p: Int, callback: HttpCallback?) {
        HttpClient.getInstance()["Im.GetSysNotice", ImHttpConst.GET_SYSTEM_MESSAGE_LIST]
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .params("p", p)
                .execute(callback)
    }



    /**
     * 获取礼物列表，同时会返回剩余的钱
     */
    @JvmStatic
    fun getGiftList(callback: HttpCallback?) {
        HttpClient.getInstance()["Gift.getGiftList", ImHttpConst.GET_GIFT_LIST]
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .execute(callback)
    }


    /**
     * 通话时候 用于检测两个用户间关系
     */
    @JvmStatic
    fun checkChatStatus(toUid: String?, callback: HttpCallback?) {
        val uid = CommonAppConfig.getInstance().uid
        val token = CommonAppConfig.getInstance().token
        val sign = MD5Util.getMD5(StringUtil.contact("token=", token, "&touid=", toUid, "&uid=", uid, "&", HttpClient.SALT))
        HttpClient.getInstance()["Live.Checkstatus", ImHttpConst.CHECK_CHAT_STATUS]
                .params("uid", uid)
                .params("token", token)
                .params("touid", toUid)
                .params("sign", sign)
                .execute(callback)
    }

    /**
     * 赠送守护礼物
     */
    @JvmStatic
    fun sendGift(liveUid: String?, sessionId: String?, giftId: String, giftCount: String?, isGuard: Boolean,
                 callback: HttpCallback?) {
        sendGift(liveUid, sessionId, giftId, giftCount, isGuard,"","", callback)
    }

    @JvmStatic
    fun sendGift(liveUid: String?, sessionId: String?, giftId: String, giftCount: String?, isGuard: Boolean,
                 groupId: String?,nick: String?,callback: HttpCallback?) {
        val type = if (isGuard) 1 else 0
        val gId = if (StringUtil.isNotEmpty(groupId)) groupId else "0"

        val sign = MD5Util.getMD5(StringUtil.contact(
                "giftid=", giftId,
                "&group_id=", gId,
                "&liveuid=", liveUid,
                "&nums=", giftCount,
                "&showid=", sessionId,
                "&to_nickname=", nick,
                "&token=", CommonAppConfig.getInstance().token,
                "&type=", type.toString(),
                "&uid=", CommonAppConfig.getInstance().uid,
                "&", HttpClient.SALT
        ))

        HttpClient.getInstance().post("Gift.SendGift", ImHttpConst.SEND_GIFT)
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .params("liveuid", liveUid)
                .params("showid", sessionId)
                .params("giftid", giftId)
                .params("nums", giftCount)
                .params("type", type)
                .params("group_id", gId)
                .params("to_nickname", nick)
                .params("sign", sign)
                .execute(callback)
    }


    @JvmStatic
    fun sendGiftIM(liveUid: String?, sessionId: String?, giftId: String, giftCount: String?, isGuard: Boolean,
                 groupId: String?,nick: String?,callback: HttpCallback?) {
        val type = if (isGuard) 1 else 0
        val gId = if (StringUtil.isNotEmpty(groupId)) groupId else "0"

        val sign = MD5Util.getMD5(StringUtil.contact(
                "giftid=", giftId,
                "&group_id=", gId,
                "&liveuid=", liveUid,
                "&nums=", giftCount,
                "&showid=", sessionId,
                "&to_nickname=", nick,
                "&token=", CommonAppConfig.getInstance().token,
                "&type=", type.toString(),
                "&uid=", CommonAppConfig.getInstance().uid,
                "&", HttpClient.SALT
        ))

        HttpClient.getInstance().post("Gift.SendGiftIM", ImHttpConst.SEND_GIFT)
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .params("liveuid", liveUid)
                .params("showid", sessionId)
                .params("giftid", giftId)
                .params("nums", giftCount)
                .params("type", type)
                .params("group_id", gId)
                .params("to_nickname", nick)
                .params("sign", sign)
                .execute(callback)
    }




    /**
     * 检测守护相关价格
     */
    @JvmStatic
    fun getGuardPrice(toUid: String?, callback: CommonCallback<GuardPriceBean?>?) {
        val uid = CommonAppConfig.getInstance().uid
        val token = CommonAppConfig.getInstance().token
        val sign = MD5Util.getMD5(StringUtil.contact("token=", token, "&touid=", toUid, "&uid=", uid, "&", HttpClient.SALT))
        HttpClient.getInstance()["Guard.GuardPrice", ImHttpConst.GET_GUARD_PRICE]
                .params("uid", uid)
                .params("token", token)
                .params("guar_id", toUid)
                .params("type", "1") //APP类型，1是安卓，2是IOS
                //                .params("sign", sign)
                .execute(object : HttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {
                        if (code == 0 && StringUtil.isNotEmpty(info)) {
                            val priceBean = GsonUtil.gsonToBean(info, GuardPriceBean::class.java)
                            callback?.callback(priceBean)
                        } else {
                            ToastUtil.show(msg)
                        }
                    }

                    override fun onError() {
                        super.onError()
                        callback?.callback(null)
                    }
                })
    }

    /**
     * 观众向主播发起通话邀请，检测主播状态，同时获取自己的推拉流地址
     *
     * @param liveUid  主播的 id
     * @param type     通话类型
     * @param callback
     */
    fun chatAudToAncStart(liveUid: String?, type: Int, callback: HttpCallback?) {
        KLog.e("chatAudToAncStart","用户向主播发起通话")
        chatAudToAncStart(liveUid, type, "", callback)
    }

    @JvmStatic
    fun chatAudToAncStart(liveUid: String?, type: Int, liveType: String?, callback: HttpCallback?) {
        val uid = CommonAppConfig.getInstance().uid
        val token = CommonAppConfig.getInstance().token
        val sign = MD5Util.getMD5(StringUtil.contact("liveuid=", liveUid, "&token=", token, "&type=", type.toString(), "&uid=", uid, "&", HttpClient.SALT))
        HttpClient.getInstance()["Live.Checklive", ImHttpConst.CHAT_AUD_TO_ANC_START]
                .params("uid", uid)
                .params("token", token)
                .params("liveuid", liveUid)
                .params("livetype", liveType)
                .params("type", type)
                .params("sign", sign)
                .execute(callback)
    }



    /**
     * 主播向观众发起通话邀请，检测观众状态，同时获取自己的推拉流地址
     */
    @JvmStatic
    fun chatAncToAudStart2(toUid: String?, type: Int, callback: HttpCallback?) {
        val uid = CommonAppConfig.getInstance().uid
        val token = CommonAppConfig.getInstance().token
        val sign = MD5Util.getMD5(StringUtil.contact("token=", token, "&touid=", toUid, "&type=", type.toString(), "&uid=", uid, "&", HttpClient.SALT))
        HttpClient.getInstance()["Live.AnchorLaunch", ImHttpConst.CHAT_ANC_TO_AUD_START_2]
                .params("uid", uid)
                .params("token", token)
                .params("touid", toUid)
                .params("type", type)
                .params("sign", sign)
                .execute(callback)
    }


    /**
     * 消息获取顶部提醒条数
     * @param callback
     */
    fun getNewFans(callback: HttpSignCallback?) {
        HttpClient.getInstance().getX("User.GetAttention", ImHttpConst.GET_CENTER)
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .execute(callback)
    }

    /**
     * 获取好友列表
     * @param callback
     */
    fun getFriendList(callback: HttpCallback?) {
        HttpClient.getInstance()["User.GetFriendsList", ImHttpConst.GET_FANS_LIST]
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .execute(callback)
    }




    /**
     * 获取通讯录好友信息
     */
    fun getContactList(json: String, callback: HttpCallback?) {
        HttpClient.getInstance().post("User.GetMailUser", ImHttpConst.GET_FANS_LIST)
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .params("mail", json)
                .execute(callback)
    }

    /**
     * 创建群
     */
    fun createChatGroup(name: String ,title: String ,num: String = "200",group_type: String = "Public" ,
                        hfIds: String , ztIds: String , callback: HttpCallback?) {
        HttpClient.getInstance().post("Group.CreateGroup", ImHttpConst.CREATE_CHAT_GROUP)
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .params("name", name)
                .params("title", title)
                .params("num", num)
                .params("group_type", group_type)
                .params("hf_uids", hfIds)
                .params("zt_uids", ztIds)
                .execute(callback)
    }

    /**
     * 搜索群组
     */
    fun getGroupSearch(key: String, p: String, callback: HttpSignCallback?) {
        HttpClient.getInstance().postX("Group.SearchGroup", ImHttpConst.GET_GROUP_SEARCH)
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .params("keyword", key)
                .params("page", p)
                .execute(callback)
    }

    /**
     * 搜索我的群组和好友
     */
    fun getIMsgSearch(key: String, callback: HttpSignCallback?) {
        HttpClient.getInstance().postX("Group.SearchGAndU", ImHttpConst.GET_GROUP_SEARCH)
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .params("keyword", key)
                .execute(callback)
    }

    /**
     * 设置为好友
     */
    fun setFriends(toUid: String) {
        HttpClient.getInstance().postX("User.SetFriends", ImHttpConst.GET_GROUP_SEARCH)
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .params("touid", toUid)
                .execute(object :HttpSignCallback(){
                    override fun onSuccess(code: Int, msg: String?, info: String?) {
                    }
                })
    }

    /**
     * 设置聊天背景
     * type 1 单聊,2 群聊,3 单对单聊
     */
    fun setIMChatBg(id: String, url: String, callback: HttpCallback?) {
        HttpClient.getInstance().post("Group.SetGroupBg", ImHttpConst.SET_GROUP_BG)
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .params("operation_id", id)
                .params("background", url)
                .params("type", "1")
                .execute(callback)
    }

    /**
     * 获取聊天背景
     * type 1 单聊,2 群聊,3 单对单聊
     */
    fun getIMChatBg(id: String, callback: HttpCallback) {
        HttpClient.getInstance().post("Group.GetGroupBg", ImHttpConst.GET_GROUP_BG)
                .params("uid", CommonAppConfig.getInstance().uid)
                .params("token", CommonAppConfig.getInstance().token)
                .params("operation_id", id)
                .params("type", "1")
                .execute(callback)
    }


}